/**
 * 
 */
package com.star.date;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * 
 * 项目名称：wyxc 类名称：DateJodaTimeUtils 类描述： 创建人：starhq 创建时间：2013-7-5 下午4:39:56
 * 修改人：starhq 修改时间：2013-7-5 下午4:39:56 修改备注：
 * 
 * @version
 * 
 */
public class DateJodaTimeUtils {

	public static final int WEEK_DAYS = 7;

	private static final String FULLFORMAT = "yyyy-MM-dd HH:mm:ss";

	private static final String DAYFORMAT = "yyyy-MM-dd";

	private static final String KANGDE = "MM/dd/yyyy hh:mm:ss aa";

	public static DateTime calendarToDateTime(Calendar c) {
		return new DateTime(c);
	}

	public static DateTime dateToDateTime(Date date) {
		return new DateTime(date);
	}

	public static DateTime longToDateTime(long time) {
		return new DateTime(time);
	}

	public static String dateTimeToFullString(DateTime dt) {
		DateTime dateTime = DateTime.now();
		if (dt != null) {
			dateTime = dt;
		}
		return dateTime.toString(FULLFORMAT);
	}

	public static String dateTimeToDayString(DateTime dt) {
		DateTime dateTime = DateTime.now();
		if (dt != null) {
			dateTime = dt;
		}
		return dateTime.toString(DAYFORMAT);
	}

	public static String dateTimeToString(DateTime dt, String format) {
		DateTime dateTime = DateTime.now();
		if (dt != null) {
			dateTime = dt;
		}
		return dateTime.toString(format);
	}

	public static DateTime stringToDateTime(String time) {
		return DateTime.parse(time, DateTimeFormat.forPattern(FULLFORMAT));
	}

	public static DateTime dayStringToDateTime(String time) {
		return DateTime.parse(time, DateTimeFormat.forPattern(DAYFORMAT));
	}

	public static DateTime customStringToDateTime(String time, String format) {
		return DateTime.parse(time, DateTimeFormat.forPattern(format)
				.withLocale(Locale.ENGLISH));
	}

	public static DateTime plusNumToDateTime(int num, char key) {
		return plusNumToDateTime(num, key, null);
	}

	public static long compareCalendarByType(Date start, Date end, char type) {
		return compareCalendarByType(dateToDateTime(start),
				dateToDateTime(end), type);
	}

	public static long compareCalendarByType(Calendar start, Calendar end,
			char type) {
		return compareCalendarByType(calendarToDateTime(start),
				calendarToDateTime(end), type);
	}

	public static long compareCalendarByType(DateTime start, DateTime end,
			char type) {
		char key = 'd';
		if (type != '\0') {
			key = type;
		}
		long diffNum = 0;

		Duration duration = new Duration(start, end);
		switch (key) {
		case 'd':
			diffNum = duration.getStandardDays();
			break;
		case 'h':
			diffNum = duration.getStandardHours();
			break;
		case 'm':
			diffNum = duration.getStandardMinutes();
			break;
		case 's':
			diffNum = duration.getStandardSeconds();
			break;
		default:
			diffNum = duration.getMillis();
			break;
		}
		return diffNum;
	}

	public static Calendar plusNumToCalendar(int num, char key, Calendar c) {
		Calendar calendar = null;
		if (c != null) {
			calendar = c;
		} else {
			calendar = Calendar.getInstance();
		}
		return plusNumToDateTime(num, key, calendarToDateTime(calendar))
				.toCalendar(Locale.getDefault());
	}

	public static DateTime plusNumToDateTime(int num, char key, DateTime dt) {
		DateTime dateTime = DateTime.now();
		if (dt != null) {
			dateTime = dt;
		}
		switch (key) {
		case 'y':
			dateTime = dateTime.plusYears(num);
			break;
		case 'M':
			dateTime = dateTime.plusMonths(num);
			break;
		case 'w':
			dateTime = dateTime.plusWeeks(num);
			break;
		case 'd':
			dateTime = dateTime.plusDays(num);
			break;
		case 'h':
			dateTime = dateTime.plusHours(num);
			break;
		case 'm':
			dateTime = dateTime.plusMinutes(num);
			break;
		case 's':
			dateTime = dateTime.plusSeconds(num);
			break;
		}
		return dateTime;
	}

	public static DateTime getStartOfDay(DateTime dt) {
		DateTime dateTime = DateTime.now();
		if (dt != null) {
			dateTime = dt;
		}
		return dateTime.withTimeAtStartOfDay();
	}

	public static DateTime getStartOfDayOfAnytime(int num, char type) {
		DateTime dateTime = new DateTime().withTimeAtStartOfDay()
				.withDayOfMonth(1);
		return plusNumToDateTime(num, type, dateTime);
	}

	public static DateTime getLastDayOfAnytime(int num, char type) {
		DateTime dateTime = new DateTime().dayOfMonth().withMaximumValue()
				.withSecondOfMinute(59).withMinuteOfHour(59).withHourOfDay(23);
		return plusNumToDateTime(num, type, dateTime);
	}

	public static void main(String[] args) {

		System.out
				.println(customStringToDateTime("8/2/2014 5:16:20 PM", KANGDE));

	}
}
